﻿using System;
using System.Linq;
using Domain.Entity;

namespace CommonDao
{
    public class QqEmailDao
    {
        /// <summary>
        /// 保存用户Email信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public static bool SaveToQQUserInfo(QQEmail dto)
        {
            using (var db = new DBDataContext())
            {
                try
                {
                    var email = db.QQEmail.FirstOrDefault(x => x.Email == dto.Email);
                    if (email == null)
                    {
                        db.QQEmail.InsertOnSubmit(dto);
                    }
                    else
                    {
                        email.OpenedQQZone = dto.OpenedQQZone == null ? email.OpenedQQZone : dto.OpenedQQZone;
                        email.OpenedQQWb = dto.OpenedQQWb == null ? email.OpenedQQWb : dto.OpenedQQWb;
                        email.NickName = string.IsNullOrWhiteSpace(dto.NickName) ? email.NickName : dto.NickName;
                        email.RealName = string.IsNullOrWhiteSpace(dto.RealName) ? email.RealName : dto.RealName;
                        email.Sex = dto.Sex == null ? email.Sex : dto.Sex;
                        email.Age = dto.Age == null ? email.Age : dto.Age;
                        email.Birthday = dto.Birthday == null ? email.Birthday : dto.Birthday;
                        email.BirthPlace = string.IsNullOrWhiteSpace(dto.BirthPlace) ? email.BirthPlace : dto.BirthPlace;
                        email.BirthPlaceDetail = string.IsNullOrWhiteSpace(dto.BirthPlaceDetail) ? email.BirthPlaceDetail : dto.BirthPlaceDetail;
                        email.LivePlace = string.IsNullOrWhiteSpace(dto.LivePlace) ? email.LivePlace : dto.LivePlace;
                        email.LivePlaceDetail = string.IsNullOrWhiteSpace(dto.LivePlaceDetail) ? email.LivePlaceDetail : dto.LivePlaceDetail;
                        email.Married = dto.Married == null ? email.Married : dto.Married;
                        email.Job = string.IsNullOrWhiteSpace(dto.Job) ? email.Job : dto.Job;
                        email.CompanyName = string.IsNullOrWhiteSpace(dto.CompanyName) ? email.CompanyName : dto.CompanyName;
                        email.CompanyAddress = string.IsNullOrWhiteSpace(dto.CompanyAddress) ? email.CompanyAddress : dto.CompanyAddress;
                        email.BlogCount = dto.BlogCount == null ? email.BlogCount : dto.BlogCount;
                        email.LiveMessageCount = dto.LiveMessageCount == null ? email.LiveMessageCount : dto.LiveMessageCount;
                        email.PictureCount = dto.PictureCount == null ? email.PictureCount : dto.PictureCount;
                        email.ShuoShuoCount = dto.ShuoShuoCount == null ? email.ShuoShuoCount : dto.ShuoShuoCount;
                        email.UpdateOn = DateTime.Now;
                    }
                    try
                    {
                        db.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
                    }
                    catch (Exception)
                    {
                        foreach (System.Data.Linq.ObjectChangeConflict occ in db.ChangeConflicts)
                        {
                            //以下是解决冲突的三种方法，选一种即可
                            // 使用当前数据库中的值，覆盖Linq缓存中实体对象的值
                            // occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
                            // 使用Linq缓存中实体对象的值，覆盖当前数据库中的值
                            occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
                            // 只更新实体对象中改变的字段的值，其他的保留不变
                            //occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
                        }
                        // 这个地方要注意，Catch方法中，我们前面只是指明了怎样来解决冲突，这个地方还需要再次提交更新，这样的话，值    //才会提交到数据库。
                        db.SubmitChanges();
                    }

                    return true;
                }
                catch (Exception ex)
                {
                    throw new Exception("保存用户Email信息失败", ex);
                }

            }

        }
        /// <summary>
        /// 判断该Email是否已经被保存过了
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool EmailHasVisited(string email)
        {
            using (var db = new DBDataContext())
            {
                if (db.QQEmail.Any(x => x.Email == email))
                    return true;
                return false;
            }
        }
        public static QQEmail GetLastQQUserEmail()
        {
            using (var db = new DBDataContext())
            {
                var last = db.QQEmail.Max(x => x.CreateOn);
                return db.QQEmail.FirstOrDefault(x => x.CreateOn == last);
            }
        }
        /// <summary>
        /// 获取未曾访问过的QQ空间
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string[] GetHasNoVisiteQQZoneUserEmail(int count)
        {
            using (var db = new DBDataContext())
            {
                var emails = db.QQEmail.Where(x => x.HasQQZoneVisited == false).Take(count).Select(x => x.Email);
                return emails.ToArray();
            }
        }

        /// <summary>
        /// 设置QQ空间已经访问过了
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static void SetQQZonHasVisited(string email)
        {
            using (var db = new DBDataContext())
            {
                var qqemail = db.QQEmail.FirstOrDefault(x => x.Email == email);
                if (qqemail != null)
                {
                    qqemail.HasQQZoneVisited = true;
                }
                db.SubmitChanges();
            }
        }
    }
}
